package whereRU.frontEnd;

import java.io.IOException;
import java.util.Calendar;



import whereRU.frontEnd.R;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;

/**
 * This class handles the alarms sent by CourseAlarmManager and sends
 * notifications to the user
 */
public class ReminderService extends IntentService {
	public static int NOTIF_ID = 0;
	private String nTitle;
	private String nMessage;
	private String nTickerMessage;
	private boolean[] courseDays;

	public ReminderService() {
		super("ReminderService");
	}

	/**
	 * This method is called when an alarm is triggered
	 */
	@Override
	protected void onHandleIntent(Intent intent) {
		courseDays = new boolean[7];
		Bundle extras = intent.getExtras();
		String days = extras.getString(CourseAlarmManager.EXTRA_COURSE_DAYS);
		String name = extras.getString(CourseAlarmManager.EXTRA_COURSE_NAME);
		long courseId = extras.getLong(CourseAlarmManager.EXTRA_COURSE_ID);
		int delay = extras.getInt(CourseAlarmManager.EXTRA_COURSE_ALARM_DELAY, 0);
		
		setCourseDays(days);

		Calendar calendar = Calendar.getInstance();
		int currentDay = calendar.get(Calendar.DAY_OF_WEEK);

		// if today is a day of the week the class 
		// takes place then send a notification
		if(courseDays[currentDay - 1] == true) {
			AssetFileDescriptor afd;
			try {
				afd = getAssets().openFd("who_who.mp3");
				MediaPlayer player = new MediaPlayer();
				player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
				player.prepare();
				player.start();
				
			} catch (IOException e) {
				
			}

			long when = calendar.getTimeInMillis();         
			NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

			nTickerMessage = "Go to class!";
			Notification notification = new Notification(R.drawable.ic_notif, nTickerMessage, when);
			notification.sound = Uri.parse("");
			notification.flags = Notification.FLAG_AUTO_CANCEL;
			Intent notificationIntent = new Intent(this, DirectionsActivity.class);
			notificationIntent.putExtra(CourseAlarmManager.EXTRA_COURSE_ID, courseId);
			PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent , 0);

			nTitle = name;
			nMessage = name + " in " + delay + " minutes.  Get directions!";
			notification.setLatestEventInfo(getApplicationContext(), nTitle, nMessage, contentIntent);
			nm.notify(NOTIF_ID++, notification);
		}
	}

	/**
	 * Adjusts the string of days to a boolean array
	 * @param days
	 */
	private void setCourseDays(String days) {
		String[] daysArray = days.split(" ");
		
		for(String d : daysArray) {
			// courseDays[0] corresponds to Sunday, so we skip it
			if(d.equals("M"))
				courseDays[1] = true;
			else if(d.equals("T"))
				courseDays[2] = true;
			else if(d.equals("W"))
				courseDays[3] = true;
			else if(d.equals("R"))
				courseDays[4] = true;
			else if(d.equals("F"))
				courseDays[5] = true;
			else if(d.equals("S"))
				courseDays[6] = true;
		}
	}
}
